package com.projectzqjz.parttimework.utils;

import android.app.Activity;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.projectzqjz.parttimework.R;

import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import me.shaohui.bottomdialog.BottomDialog;

public class ShowShareUtils {

    private static Activity activity;
    private static String title = "";
    private static String description = "";
    private static String icon = "";
    private static String url = "";

    /**
     * @param act     必须是MainActivity,其他的Activity，必须实现QQ的分享接口MyIUiListener
     * @param manager FragmentManager
     * @param object  要分享的内容，类型为对象
     */
    public static void showShareDialog(final Activity act, FragmentManager manager, JSONObject object) {
        activity = act;
        if (object == null) {
            ToastUtil.showShort(act, "分享失败");
            return;
        }
        shareGoods(JSONObject.parseObject(object.toString()));
        final BottomDialog bottomDialog;
        bottomDialog = BottomDialog.create(manager);
        bottomDialog.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });
                //微信
                v.findViewById(R.id.share_weiChat).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareToPlatform(1);
                        /*
                        String wxOpenId = SPUtil.get(act.getApplicationContext(), Constant.WxInfo.WX_OPEN_ID, "").toString();
                        if (TextUtils.isEmpty(wxOpenId)) {
                            ToastUtil.showShort(act, "请先进行微信授权");
                            return;
                        }
                        shareToWXORFriend(0);
                        */
                        bottomDialog.dismiss();
                    }
                });
                //微信朋友圈
                v.findViewById(R.id.share_circleFriends).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareToPlatform(2);
                        /*
                        String wxOpenId = SPUtil.get(act.getApplicationContext(), Constant.WxInfo.WX_OPEN_ID, "").toString();
                        if (TextUtils.isEmpty(wxOpenId)) {//先授权
                            ToastUtil.showShort(act, "请先进行微信授权");
                            return;
                        }
                        shareToWXORFriend(0);
                        */
                        bottomDialog.dismiss();
                    }
                });
                //QQ
                v.findViewById(R.id.share_qq).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareToPlatform(3);
                        bottomDialog.dismiss();
                    }
                });
                //微博
                v.findViewById(R.id.share_weiBo).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareToPlatform(4);
                        bottomDialog.dismiss();
                    }
                });
            }
        }).setLayoutRes(R.layout.share_dialog)
                .setDimAmount(0.4f)            // Dialog window dim amount(can change window background color）, range：0 to 1，default is : 0.2f
                .setCancelOutside(true)     // click the external area whether is closed, default is : true
                .setTag("BottomDialog")     // setting the DialogFragment tag
                .show();
    }

    /**
     * 解析商品
     */
    private static void shareGoods(JSONObject jsonObject) {
        title = jsonObject.getString("title");
        description = jsonObject.getString("description");
        icon = jsonObject.getString("icon");
        url = jsonObject.getString("url");
    }


    /**
     * ShareSDK分享到各个平台
     */
    private static void shareToPlatform(int platform) {
        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        oks.setTitle(title);
        oks.setTitleUrl(url);
        oks.setText(description);
        oks.setUrl(url);
        oks.setImageUrl(icon);
        switch (platform) {
            case 1://微信
                oks.setPlatform(Wechat.NAME);
                break;
            case 2://微信朋友圈
                oks.setPlatform(WechatMoments.NAME);
                break;
            case 3://QQ
                oks.setPlatform(QQ.NAME);
                break;
            case 4://微博
                oks.setPlatform(SinaWeibo.NAME);
                break;
        }
        oks.show(activity);
    }

}
